package com.rules.admin.controller;


import com.rules.admin.entity.DataSourceModel;
import com.rules.admin.service.DataSourceModelService;
import com.rules.admin.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping({"/dataSource"})
@Controller
public class DataSourceManagerController {
   @Autowired
   private DataSourceModelService dataSourceModelService;

   @RequestMapping(
      value = {"datasource_list/{id}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getDataSourceList(@PathVariable("id") String ProjectId) {
      return ResultUtils.getSuccessResultData(this.dataSourceModelService.selectListByProjectId(ProjectId));
   }

   @RequestMapping(
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> addDateSource(@RequestBody DataSourceModel dataSourceModel) {
      this.dataSourceModelService.save(dataSourceModel);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> saveDateSource(@RequestBody DataSourceModel dataSourceModel) {
      this.dataSourceModelService.save(dataSourceModel);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getDateSource(@PathVariable("id") String id) {
      DataSourceModel dataSourceModel = (DataSourceModel)this.dataSourceModelService.getById(Long.parseLong(id));
      return ResultUtils.getSuccessResultData(dataSourceModel);
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteDateSource(@PathVariable("id") String id) {
      this.dataSourceModelService.removeById(Long.parseLong(id));
      return ResultUtils.getSuccessResultData();
   }
}
